const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
const baseURL = 'https://erp.tuocaiyun.com/prod-api';
// const baseURL = 'http://127.0.0.1:8080';
function request(method, url, data,contentType) {
    let token = uni.getStorageSync('token');
    return new Promise(function (resolve, reject) {
        if (method == 'PUT') {
            let datas = data.getData();
            let header = {
                'content-type': datas.contentType,
                Authorization: token
            };
            uni.request({
                url: baseURL + url,
                method: method,
                data: datas.buffer,
                header: header,
                success(res) {
                    console.log(res.data);
                    //请求成功
                    if (res.data.code == 200) {
                        resolve(res.data);
                    } else if (res.data.code == 401) {
                        uni.clearStorage('token');
                    } else {
                        resolve(res.data);
                    }
                }
            });
        } else {
            let header = {
                'content-type': contentType,
                Authorization: token
            };
            uni.request({
                url: baseURL + url,
                method: method,
                data: Object.assign({}, data),
                header: header,
                success(res) {
                    console.log(res.data);
                    //请求成功
                    if (res.data.code == 200) {
                        resolve(res.data);
                    } else if (res.data.code == 401) {
                        uni.clearStorage('token');
                    } else {
                        resolve(res.data);
                    }
                }
            });
        }
    });
}
const API = {
    // 登陆更新用户信息
    goLogin: (data) => request(POST, '/login', data,'application/json'),
    // 获取code
    captchaImage: (data) => request(GET, '/captchaImage', data,'application/json'),
    // 获取个人信息
    getInfo: (data) => request(GET, '/getInfo', data,'application/json'),
    // 修改密码
    updatePassword: (data) => request(PUT, '/system/user/profile/updatePwd', data,'application/json'),
    // 获取openId
    wxLogin: (data) => request(GET, '/wx.login', data,'application/json'),
    // 支付
    // payment: (data) => request(POST, '/third/liantuofu/payment', data)
	payment: (data) => request(POST, '/third/jiquan/UnifiedOrder', data,'application/json'),
	// 支付列表
	paymentList: (data) => request(POST, '/third/jiquan/UnifiedOrder/list', data,'application/x-www-form-urlencoded;charset=utf-8'),
};
module.exports = {
    API: API
};
